package com.example.springsecuritystart.controller;

import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.Map;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(HttpServletRequest req) {
        final String localAddr = req.getLocalAddr();
        final String localName = req.getLocalName();
        final int localPort = req.getLocalPort();
        final Enumeration<String> parameterNames = req.getParameterNames();
        final Map<String, String[]> parameterMap = req.getParameterMap();
        final String protocol = req.getProtocol();
        final String remoteAddr = req.getRemoteAddr();
        final String remoteHost = req.getRemoteHost();
        final String serverName = req.getServerName();
        final int serverPort = req.getServerPort();
        final HttpSession session = req.getSession();
        System.out.println("sessionId = " + session.getId());
        return "hello spring security";
    }

    @GetMapping("/https")
    public String https() {
        String msg = "hellos spring security";
        return msg;
    }

    @GetMapping("/encrypt")
    public String encrypt() {
        String msg = "hellos spring security";
        final String encrypt = Encryptors.text("123456", "654321").encrypt(msg);
        return encrypt;
    }

    @GetMapping("/decrypt")
    public String decrypt(String text) {
        final String decrypt = Encryptors.text("123456", "654321").decrypt(text);
        return decrypt;
    }
}
